#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexEnAmPm(L" (\\d+(\\.\\d+)*) (am|pm)");

// X pm
void TextNormalizer::TnReEnAmPm(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, mcWregexEnAmPm))
	{
		string tStrEnAmPm = wstring2utf8string(tWsmatch[0]);		//匹配到的字符串
		StrReplace(tStrEnAmPm, " ", "");

		string tStrNumber = "";
		string tStrSuffix = "";
		for(const char tcChar : tStrEnAmPm) {
			if( (tcChar >= '0' && tcChar <= '9') || (tcChar == '.') ) {
				tStrNumber += tcChar;
			}
			else {
				tStrSuffix += tcChar;
			}
		}

		bool tbIsSuffixNoChar = true;
		size_t tcBackNextId = tWsmatch.position(0) + tWsmatch.length(0);
		if(tcBackNextId < fpInWsentence->size()) {
			wchar_t tWcharBackNext = fpInWsentence->at(tcBackNextId);
			if(tWcharBackNext >= L'a' && tWcharBackNext <= L'z') {
				tbIsSuffixNoChar = false;
			}
		}

		string tStrReplace = "";
		vector<string> tcVecDotItem = StrSplit(tStrNumber, ".");
		if(tbIsSuffixNoChar && tcVecDotItem.size() <= 2) {
			tStrReplace += ReplaceMethod(tcVecDotItem, (fcInLanguage==LANG_EN)?RME_en_colon_dianfen:RME_zh_colon_dianfen);
			if(tStrSuffix == "am" || tStrSuffix == "pm") {
				tStrReplace += " ";
				for(const char tcChar : tStrSuffix) {
					tStrReplace += tcChar;
					tStrReplace += " ";
				}
			}
		}
		else {
			tStrReplace = tStrNumber + "   " + tStrSuffix;
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
